//
//  CZTabBarButton.m
//  传智微博
//
//  Created by apple on 15-3-5.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "CZTabBarButton.h"
#import "CZBadgeView.h"



#define CZImageRidio 0.4

@interface CZTabBarButton ()

@property (nonatomic, weak) CZBadgeView *badgeView;

@end

@implementation CZTabBarButton

// 重写setHighlighted，取消高亮做的事情
- (void)setHighlighted:(BOOL)highlighted{}

// 懒加载badgeView
- (CZBadgeView *)badgeView
{
    if (_badgeView == nil) {
        CZBadgeView *btn = [CZBadgeView buttonWithType:UIButtonTypeCustom];
        
        [self addSubview:btn];
        
        _badgeView = btn;
    }
    
    return _badgeView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
       UIColor *textcolor= [[UIColor alloc]initWithRed:46/255.0 green:141/255.0 blue:215/255.0 alpha:1.0];
          UIColor *textcolornormal= [[UIColor alloc]initWithRed:114/255.0 green:114/255.0 blue:114/255.0 alpha:1.0];
        // 设置字体颜色
        [self setTitleColor:textcolornormal forState:UIControlStateNormal];
        [self setTitleColor:textcolor forState:UIControlStateSelected];
        
        // 图片居中
        
        self.imageView.contentMode=UIViewContentModeScaleAspectFit;
        // 文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 设置文字字体
        self.titleLabel.font = [UIFont systemFontOfSize:10];
        self.backgroundColor=[UIColor clearColor];
        
    }
    return self;
}
// 传递UITabBarItem给tabBarButton,给tabBarButton内容赋值
- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];

    // KVO：时刻监听一个对象的属性有没有改变
    // 给谁添加观察者
    // Observer:按钮
    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionNew context:nil];
    
}

// 只要监听的属性一有新值，就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    [self setTitle:_item.title forState:UIControlStateNormal];
    
    [self setImage:_item.image forState:UIControlStateNormal];
    
    [self setImage:_item.selectedImage forState:UIControlStateSelected];
    
    // 设置badgeValue
    self.badgeView.badgeValue = _item.badgeValue;
}

// 修改按钮内部子控件的frame
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.imageView
    CGFloat imageX = 0;
    CGFloat imageY = 8;
    CGFloat imageW = self.bounds.size.width;
    CGFloat imageH = 22;
    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    
    
    // 2.title
    CGFloat titleX = 0;
    CGFloat titleY = imageH+8;
    CGFloat titleW = self.bounds.size.width;
    CGFloat titleH = self.bounds.size.height - titleY;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    CGFloat constent;
    if (XHScreenwidth==320) {
      constent =12;
    }else{
        constent=24;
    }
    // 3.badgeView
    self.badgeView.x = self.width - self.badgeView.width - constent;
    self.badgeView.y = 0;
}
-(void)dealloc{
  
    [self.item removeObserver:self forKeyPath:@"badgeValue" context:nil];
    [self.item removeObserver:self forKeyPath:@"selectedImage" context:nil];
    [self.item removeObserver:self forKeyPath:@"image" context:nil];
     [self.item removeObserver:self forKeyPath:@"title" context:nil];
  
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
